#include "StdAfx.h"

TilesetUI::TilesetUI(TexSelUI* pSel) : m_pTexSel(pSel)
{
	m_dialog = new Dialog(50, 50, 9, 6, "Tilesets");
	CMpqFile f("Tilesets.txt");
	char* pBuffer = new char[f.GetSize()];
	f.Read(0, f.GetSize(), pBuffer);
	std::string files = pBuffer;
	delete [] pBuffer;
	std::string::size_type pos;
	ui32 i = 0;
	while((pos = files.find('\n')) != std::string::npos)
	{
		std::string tmp = files.substr(0, pos - 1);
		Checkbox<TilesetUI>* pCheck = m_dialog->AddCheckbox(this, 20, 30 + i * 25, tmp, &TilesetUI::CheckBoxProc);
		pCheck->m_visible = i < 5;
		m_pBoxes.push_back(pCheck);
		files = files.substr(pos + 1, files.length());
		++i;
	}
	if(i > 6)
	{
		m_dialog->AddVScrollbar(256, 50, 120, this, &TilesetUI::ScrollBarProc)->SetRange(0, i - 6);
	}
	m_curPos = 0;
	sUI.AddElement(m_dialog);
}

void TilesetUI::CheckBoxProc(Checkbox<TilesetUI>* pBox, ui32 c)
{
	if(!c)
		return;
	for(ui32 i = 0; i < m_pBoxes.size(); ++i)
	{
		if(m_pBoxes[i] == pBox)
			continue;
		m_pBoxes[i]->m_state = 0;
	}
	m_pTexSel->SetZone(pBox->GetText());
}

void TilesetUI::ScrollBarProc(VScrollbar<TilesetUI>*, ui32 v)
{
	m_curPos = v;
	for(ui32 i = 0; i < m_pBoxes.size(); ++i)
	{
		m_pBoxes[i]->m_visible = i >= m_curPos && (i - m_curPos) < 5;
		if(m_pBoxes[i]->m_visible)
			m_dialog->SetCheckPos(m_pBoxes[i], 20, (i - m_curPos) * 25 + 30);
	}
}